﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VMC.Domain.Model.Service.Abstract;
using VMC.Domain.Model.Service;
using VMC.Web.Logic;
using VMC.Domain.Model.DataStructure;


namespace VMC.Web.Controllers.DataStructure
{
    [Authorize]
    public class DataTypesController : Controller
    {
        private IComplexDataTypeModelService iComplex;
        private const string IndexView = "~/Views/DataTypes/Index.cshtml";
        private const string ColumnView = "~/Views/DataTypes/_AddEditComplexDataTypeColumn.cshtml";
        
        public DataTypesController() : this(new ComplexDataTypeModelService()) { }
        public DataTypesController(IComplexDataTypeModelService ComplexDataTypeService)
        {
            iComplex = ComplexDataTypeService;
        }

        public ActionResult Index()
        {
            iComplex.UserName = User.Identity.Name;
            return View(IndexView, iComplex.GetList(User.Identity.Name));
        }
        public ActionResult Update(int ID)
        {
            iComplex.UserName = User.Identity.Name;
            return View(IndexView, iComplex.GetUpdateModel(ID));
        }
        [HttpPost]
        public ActionResult Update(AddEditComplexDataType Model)
        {
            Model.UserName = User.Identity.Name;
            if (ModelState.IsValid)
            {
                if (iComplex.Update(Model))
                    return RedirectToAction("Index");
                else
                    ModelState.MergeServiceErrors(iComplex.GetErrors());
            }
            return View(IndexView, iComplex.GetCurrentModel(Model));
        }

        public ActionResult Create()
        {
            iComplex.UserName = User.Identity.Name;
            return View(IndexView, iComplex.GetCreateModel());
        }

        [HttpPost]
        public ActionResult Create(AddEditComplexDataType Model)
        {

            Model.UserName = User.Identity.Name;
            Model.IsPublic = false;
            if (ModelState.IsValid)
            {
                if (iComplex.Create(Model))
                    return RedirectToAction("Index");
                else
                    ModelState.MergeServiceErrors(iComplex.GetErrors());
            }
            return View(IndexView, iComplex.GetCurrentModel(Model));
        }

        public ActionResult Delete(int ID)
        {
            iComplex.Delete(ID);
            return RedirectToAction("Index");
        }



        public ActionResult Columns(int ID)
        {
            iComplex.UserName = User.Identity.Name;

            HttpCookie dtInfo = new HttpCookie("DataTypeInformation");
            dtInfo["ColumnID"] = ID.ToString();
            dtInfo.Expires = System.DateTime.Now.AddMinutes(15);
            Response.Cookies.Add(dtInfo);

            return View(IndexView, iComplex.GetColumnsForEditing(ID, User.Identity.Name));
        }

        [HttpPost]
        public ActionResult Columns(AddEditComplexDataTypeColumn Model)
        {
            return RedirectToAction("Columns", new { ID = Model.ComplexDataTypeID });
        }

        [HttpPost]
        public ActionResult AddBasicColumn(AddEditComplexDataTypeColumn Model)
        {
            if (ModelState.IsValid)
            {
                if (Model.ID > 0)
                {
                    //This means we need to update the column not inserting it
                    iComplex.UpdateBasicDataTypeColumn(Model);
                }
                else if (iComplex.AddBasicDataTypeColumn(Model))
                {
                }
                else
                    ModelState.MergeServiceErrors(iComplex.GetErrors());
            }
            
            //Return the view with errors
            //return View(ColumnView, Model);

            //return Content("Jigar Patel");
            //return RedirectToAction("Columns", new { ID = Model.ComplexDataTypeID });
            HttpCookie dtInfo = Request.Cookies["DataTypeInformation"];
            int cid = 0;
            if (dtInfo != null)
            {
                cid = int.Parse(dtInfo["ColumnID"]);
            }
            return RedirectToAction("Columns", new { ID = cid });
        }


        [HttpPost]
        public ActionResult UpdateColumnOrder(int id, FormCollection Form)
        {
            HttpCookie dtInfo = Request.Cookies["DataTypeInformation"];
            if (dtInfo != null)
            {
                id = int.Parse(dtInfo["ColumnID"]);
            }
            string[] changes = Form.GetValue("changedorder").AttemptedValue.Split(new char[]{'|'} , StringSplitOptions.RemoveEmptyEntries);
            foreach (string change in changes)
            {
                string[] ids = change.Split('-');
                iComplex.UpdateColumnOrder(id, Convert.ToInt32(ids[0]), Convert.ToInt32(ids[1]));
            }
            return RedirectToAction("Columns", new { ID = id });
        }

        public ActionResult UpdateColumn(int id)
        {
            iComplex.UserName = User.Identity.Name;
            HttpCookie dtInfo = Request.Cookies["DataTypeInformation"];
            ComplexDataTypeManagement mManage = new ComplexDataTypeManagement();
            if(dtInfo != null)
            {
                 mManage = iComplex.GetColumnsForEditing( int.Parse(dtInfo["ColumnID"]),User.Identity.Name);
            }
            mManage.ColumnManagement.AddEditModel = iComplex.GetUpdateColumnModel(id, User.Identity.Name).ColumnManagement.AddEditModel;
            mManage.ColumnManagement.AddEditModel.ComplexDataTypeID = int.Parse(dtInfo["ColumnID"]);
            return View(IndexView, mManage);
        }
        public ActionResult DeleteColumn(int id)
        {
            HttpCookie dtInfo = Request.Cookies["DataTypeInformation"];
            int cid = 0;
            if (dtInfo != null)
            {
                cid = int.Parse(dtInfo["ColumnID"]);
            }
            iComplex.DeleteBasicDataTypeColumn(id);
            iComplex.RefreshColumnOrder(cid);
            //Now delete the column and column mapping
            return RedirectToAction("Columns", new { ID = cid});
        }
    }
}
